package services

import (
	"time"

	"cashew.com/cashew_common/utils"
	"github.com/beego/beego/v2/core/logs"
	"github.com/pkg/errors"
)

/*type CleanUsers struct {
}
*/

/*
func (c *CleanUsers) Run(mainWg *sync.WaitGroup) {

	cr := cron.New(cron.WithSeconds())
	go func() {
		cr.AddFunc("0 0 5 * * *", c.cleanUsers)
		cr.Start()
	}()
}
*/

func CleanUsers() error {
	logs.Info("cleanUsers::clean users::time:", time.Now().Format("2006-01-02 15:04:05"))
	db := utils.GetDB()
	if db == nil {
		logs.Error("cleanUsers::db is nil")
		return errors.New("db is nil")
	}
	_, err := db.Exec("DELETE FROM users WHERE status = 1 AND update_time < UNIX_TIMESTAMP(NOW() - INTERVAL 2 MONTH)")
	if err != nil {
		logs.Error("cleanUsers::clean users failed::exec error:", err)
		return err
	}

	return nil
}
